package com.adcc.ags.cgw.configuration;

import com.adcc.ags.cgw.App;
import com.adcc.ags.cgw.entity.user.BlackListUser;
import com.adcc.ags.cgw.entity.user.SpecLabel;
import com.adcc.ags.cgw.utils.Constant;
import com.adcc.ags.cgw.utils.FileUtil;
import com.adcc.utility.log.Log;
import com.adcc.utility.xml.*;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 黑名单用户列表配置工厂
 */
public class BlackListFactory {

    // 单例
    private static BlackListFactory instance;

    // 用户配置列表
    private List<BlackListUser> blackList = Collections.synchronizedList(new ArrayList<BlackListUser>());

    /**
     * 构造函数
     */
    private BlackListFactory(){

    }

    /**
     * 单例方法
     * @return
     */
    public synchronized static BlackListFactory getInstance(){
        if(instance == null){
            instance = new BlackListFactory();
        }
        return instance;
    }

    public List<BlackListUser> getBlackList() {
        return blackList;
    }

    /**
     * 解析黑名单用户配置列表
     * @param adccxml
     * @param flag (true:读取目标黑名单用户列表,false:读取当前黑名单用户列表)
     * @return
     * @throws Exception
     */
    public List<BlackListUser> parseBlackList(ADCCXML adccxml, boolean flag)throws Exception {
        try {
            List<BlackListUser> blackListUserList = Lists.newArrayList();
            SuperNode userNode = adccxml.getSuperNode("blackList");
            for(Node node : userNode.getNodeList()) {
                if(node.getAttributes().containsKey("id") && node.getAttributes().containsKey("name")) {
                    String strID = node.getAttributes().get("id").trim();
                    String strName = node.getAttributes().get("name").trim();
                    BlackListUser blackListUser = new BlackListUser(strID,strName);
                    if(node.getUnitList().size() > 0) {
                        for(Unit unit : node.getUnitList()) {
                            if("SendAddress".equals(unit.getAttributes().get("name").trim())){
                                blackListUser.setSendAddress(unit.getText().trim());
                            }
                            if("RecvAddress".equals(unit.getAttributes().get("name").trim())){
                                String srtRecvAddress = unit.getText().trim();
                                if(!Strings.isNullOrEmpty(srtRecvAddress)){
                                    blackListUser.setRecvAddress(srtRecvAddress.split(","));
                                }
                            }
                            if("SMI".equals(unit.getAttributes().get("name").trim())){
                                String strSMI = unit.getText().trim();
                                if(!Strings.isNullOrEmpty(strSMI)){
                                    blackListUser.setSmi(strSMI.split(","));
                                }
                            }
                            if("AN".equals(unit.getAttributes().get("name").trim())){
                                String strAN = unit.getText().trim();
                                if(!Strings.isNullOrEmpty(strAN)){
                                    blackListUser.setAn(strAN.split(","));
                                }
                            }
                            if("SpecLabel".equals(unit.getAttributes().get("name").trim())){
                                SpecLabel specLabel = new SpecLabel();
                                specLabel.setContent(unit.getText().trim());
                                try {
                                    String strIndex = unit.getAttributes().get("index").trim();
                                    if(!Strings.isNullOrEmpty(strIndex)) {
                                        specLabel.setIndex(Integer.parseInt(strIndex));
                                    }
                                } catch (Exception e) {
                                    Log.error(BlackListFactory.class.getName(), "parse xml error reason: invalid speclabel index");
                                }
                                blackListUser.setSpecLabel(specLabel);
                            }
                        }
                    }
                    blackListUser.setEmergency(flag);
                    blackListUserList.add(blackListUser);
                }
            }
            return blackListUserList;
        } catch (Exception ex) {
            Log.error(BlackListFactory.class.getName(), "parseBlackList() error", ex);
            throw ex;
        }
    }

    /**
     * 加载黑名单
     */
    public ADCCXML load() {
        try {
            ADCCXML adccxml = null;
            String strBlackListFile = App.confPath + File.separator + Constant.STRING_BLACKLIST_FILE;
            if (FileUtil.isExist(strBlackListFile)) {
                adccxml = ADCCXMLFactory.getInstance().createADCCXML();
                adccxml.parseXMLFile(strBlackListFile);
                List<BlackListUser> list = parseBlackList(adccxml, false);
                if(blackList.size() > 0){
                    blackList.clear();
                }
                if(list != null && list.size() > 0){
                    for(BlackListUser u : list){
                        blackList.add(u);
                    }
                }
            }
            return adccxml;
        } catch (Exception ex) {
            Log.error(BlackListFactory.class.getName(), "load() error", ex);
            return null;
        }
    }

    /**
     * 加载黑名单
     */
    public ADCCXML load(ADCCXML adccxml) {
        try {
            if (adccxml != null) {
                List<BlackListUser> list = parseBlackList(adccxml, false);
                if(blackList.size() > 0){
                    blackList.clear();
                }
                if(list != null && list.size() > 0){
                    for(BlackListUser u : list){
                        blackList.add(u);
                    }
                }
            }
            return adccxml;
        } catch (Exception ex) {
            Log.error(BlackListFactory.class.getName(), "load() error", ex);
            return null;
        }
    }

    /**
     * 保存黑名单
     * @param adccxml
     * @return
     */
    public void save(ADCCXML adccxml) {
        try {
            String strBlackListFile = App.confPath + File.separator + Constant.STRING_BLACKLIST_FILE;
            adccxml.createXMLFile(strBlackListFile, XMLPattern.PRETTY);
        } catch (Exception ex) {
            Log.error(BlackListFactory.class.getName(), "save() error", ex);
        }
    }

    /**
     * 添加黑名单用户
     * @param user
     */
    public void addBlackListUser(BlackListUser user) {
        if(blackList != null && blackList.size() > 0){
            for(BlackListUser u : blackList){
                if(u.getId().equals(user.getId()) && u.getName().equals(user.getName())){
                    return;
                }
            }
        }
        blackList.add(user);
    }
}
